source('codes/00_import_libraries.R')



#------------------------------------------------------------------------#
# Specifications ----
#------------------------------------------------------------------------#

PLS    = 'PLS_All'
dict_name = 'base'
topic_list = readRDS('input/narrative_dicts_v2.rds')[['base']] %>% 
  names()
row_names = topic_list %>% str_replace_all('_', ' ') %>% tools::toTitleCase()



#------------------------------------------------------------------------#
# Data ----
#------------------------------------------------------------------------#

tb = readRDS('input/combined_data.rds')[[dict_name]] %>% 
  select(-year)

tb_stat = tb %>%
  select(all_of(c(topic_list, PLS))) %>%
  drop_na()



#------------------------------------------------------------------------#
# Statistics ----
#------------------------------------------------------------------------#

#-----------------------------#
#----- mean, sd, quartiles ---#
#-----------------------------#

stat = apply(tb_stat*100, 2, function(x) 
  c(length(x), mean(x), sd(x), quantile(x, c(0.25, 0.5, 0.75)))
)

stat = as_hux(stat) %>%
  insert_column(c('N','Mean','SD','Q1','Median','Q3')) %>%
  insert_row('Panel B: Summary Statistic', fill = '') %>%
  merge_cells(1, everywhere) %>%
  set_top_border(1, everywhere, 0.8) %>%
  set_bottom_border(final(1), everywhere, 0.8)



#-----------------------------#
#----- autocorrelations ------#
#-----------------------------#

ac = apply(tb_stat, 2, function(x) acf(x, 4, plot=FALSE)$acf[2:5]) * 100

ac = as_hux(ac) %>%
  insert_column(c('AC(1)','AC(2)','AC(3)','AC(4)')) %>%
  insert_row('Panel C: Autocorrelations', fill = '') %>%
  merge_cells(1, everywhere) %>%
  set_top_border(1, everywhere, 0.8) %>%
  set_bottom_border(final(1), everywhere, 0.8)



#-----------------------------#
#----- pls weitghs -----------#
#-----------------------------#

topic_used = topic_list
pi = fn_construct_pls_full(
  df   = tb,
  xvar = topic_used,
  yvar = 'mkt1',
  scale = FALSE,
  )$w * 100

names(pi) = topic_used

wts = pi[c(topic_list, 'PLS')]

wts = as_hux(t(wts)) %>%
  insert_column('PLS Weights') %>%
  insert_row('Panel D: PLS Weights', fill = '') %>%
  merge_cells(1, everywhere) %>%
  set_top_border(1, everywhere, 0.8) %>%
  set_bottom_border(final(1), everywhere, 0.8)



#-----------------------------#
#----- correlations ----------#
#-----------------------------#

cor  = cor(tb_stat) * 100
diag(cor) = NA

cor = as_huxtable(cor) %>%
  insert_column(c(row_names, 'PLS')) %>%
  insert_row(c('', row_names, 'PLS')) %>% 
  set_number_format(1) %>% 
  set_align(everywhere, -1, 'center') %>% 
  set_tb_borders(1, everywhere, 0.8) %>%
  set_bottom_border(final(1), everywhere, 0.8) %>% 
  set_all_padding(0)



#------------------------------------------------------------------------#
# Combine results ----
#------------------------------------------------------------------------#

output = cbind(
  stat[2:7, ] %>% t(),
  ac[2, ] %>% t(),
  wts[2, ] %>% unname() %>% t(),
  cor[16, ] %>% t()
) %>% 
  insert_column(c('', row_names, 'PLS')) %>% 
  set_number_format(-1, everywhere, 2) %>% 
  set_number_format(everywhere, 2, 0) %>% 
  set_lr_borders(0) %>% 
  set_tb_borders(1, everywhere, 0.8) %>% 
  set_bottom_border(final(1), everywhere, 0.8) %>% 
  set_all_padding(0) %>% 
  set_align(everywhere, -1, 'center')

output[1, 10] = 'Corr PLS'


#------------------------------------------------------------------------#
# Export ----
#------------------------------------------------------------------------#

quick_xlsx(output, file = glue('output/tables/Table_2.xlsx'))
# cat(to_latex(output, tabular_only = TRUE), file = glue('output/tables/Table_2.tex'))


